package com.ucode.springboot.starter.fileStorage.provider.aliyun;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ucode.springboot.starter.fileStorage.FsProvider;
import cn.hutool.core.map.MapUtil;

/**
 * 阿里云OSS配置
 */
//@RefreshScope
@EnableConfigurationProperties
@Configuration
@ConfigurationProperties(prefix = "ucode.fs.aliyun")
public class AliyunConfig {

    private Boolean enable = false;
    private Map<String, AliyunProperties>  clients;
    
    @Bean(name="aliyunFsProviderMap")
    public Map<String, FsProvider> aliyunFsProviderMap(){
        
        Map<String, FsProvider> groupMap = null;
        if(enable && MapUtil.isNotEmpty(clients)){
            groupMap = new HashMap<String, FsProvider>();
            for(Map.Entry<String,AliyunProperties> entry : clients.entrySet()){
                String key = entry.getKey();
                AliyunProperties properties = entry.getValue();
                if(properties != null && properties.getEnable()){
                    AliyunossProvider provider = new AliyunossProvider(key,properties.getUrlprefix(), properties.getEndpoint(),properties.getBucket(),  properties.getAccessKey(),properties.getSecretKey(), properties.getIsprivate());
                    groupMap.put(key, provider);
                }
            }
        }
        return groupMap;
    }
    
    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public Map<String, AliyunProperties> getClients() {
        return clients;
    }

    public void setClients(Map<String, AliyunProperties> clients) {
        this.clients = clients;
    }
}